#include "p2head.h"

//extern Lock doctorLock;
//extern Condition doctorCondition;
extern Lock nurseLock;
extern Condition nurseCondition;
extern doctorOfficeMonitor dom;
extern Lock* doctorLock[4];
extern Condition* doctorCondition[4];
extern doctorOfficeMonitor* doctoroffice[4];
extern struct examSheet* wallpoket[7];
extern Lock* roomLock[6];
extern Condition* roomCondition[6];
extern examinationRoomMoitor erm;
extern Lock wallpoketLock;
extern int testcontrol;
extern bool doctormv[6];

void doctor(int which){
	int roomnumber;
	int y;
	int r;

	
	struct examSheet* mysheet;
	
	srand((int)time(NULL));
	while(true){

		doctorLock[0]->Acquire();
		dom.doctorstatus[which]=1;
		doctorLock[0]->Release();

		nurseLock.Acquire();
		if(dom.nurseCount>0){						
			nurseCondition.Signal(&nurseLock);
			dom.nurseCount--;
		}
		nurseLock.Release();
			
		doctorLock[which]->Acquire();

		doctormv[which]=true;
		doctorCondition[which]->Wait(doctorLock[which]);
		doctormv[which]=false;
		
		roomnumber = doctoroffice[which]->roomNumber;
		mysheet = doctoroffice[which]->sheet;
		
		if(testcontrol==6||testcontrol==7)
			printf("Doctor [%d] is leaving their office.\n",which);
		doctorLock[which]->Release();

		/*******************in the exroom*******************************/	
		
		if(testcontrol==6||testcontrol==7)
			if(mysheet->patientType==1)
				printf("Doctor [%d] is reading the examination sheet of [Adult] Patient [%d] in Examination room [%d].\n",which,mysheet->name,roomnumber);
			else
				printf("Doctor [%d] is reading the examination sheet of [Child] Patient [%d] in Examination room [%d].\n",which,mysheet->name,roomnumber);

		roomLock[roomnumber]->Acquire();
		if(mysheet->status==1){
			
			r=rand();
			
			mysheet->shot=r%2;			
			mysheet->xray=r%4;
			
			if(testcontrol==6||testcontrol==7)
				if(mysheet->shot==1)
					if(mysheet->patientType==1)
						printf("Doctor [%d] notes down in the sheet that [Adult] Patient [%d] needs to be given a shot in Examination room [identifier].\n",which,mysheet->name);
					else
						printf("Doctor [%d] notes down in the sheet that [Child] Patient [%d] needs to be given a shot in Examination room [identifier].\n",which,mysheet->name);
			
			if(testcontrol==6||testcontrol==7)
				if(mysheet->xray!=0)
					if(mysheet->patientType==1)
						printf("Doctor [%d] notes down in the sheet that Xray is needed for [Adult] Patient [%d] in Examination room [identifier].\n",which,mysheet->name);
					else
						printf("Doctor [%d] notes down in the sheet that Xray is needed for [Child] Patient [%d] in Examination room [identifier].\n",which,mysheet->name);
			
		}
		else{
			
			if(testcontrol==6||testcontrol==7)
				if(mysheet->xray!=0)
					if(mysheet->patientType==1)
						printf("Doctor [%d] is examining the Xrays of [Adult] Patient [%d] in Examination room [%d].\n",which,mysheet->name,mysheet->exRoomNum);
					else
						printf("Doctor [%d] is examining the Xrays of [Child] Patient [%d] in Examination room [%d].\n",which,mysheet->name,mysheet->exRoomNum);

			mysheet->status=0;
		}
		
		if(testcontrol==6||testcontrol==7)
			if((mysheet->shot==0&&mysheet->xray==0)||mysheet->status==0)
				if(mysheet->patientType==1)
					printf("Doctor [%d] diagnoses [Adult] Patient [%d] to be fine and is leaving Examination Room [%d].\n",which,mysheet->name,mysheet->exRoomNum);
				else
					printf("Doctor [%d] diagnoses [Child] Patient [%d] to be fine and is leaving Examination Room [%d].\n",which,mysheet->name,mysheet->exRoomNum);


		mysheet->doctorid=which;
		
		wallpoketLock.Acquire();	//lock the wall poket to change it.
		wallpoket[roomnumber]=mysheet;	//put the sheet in the wall pocket
		erm.doctorid[roomnumber]=which;
		roomCondition[roomnumber]->Signal(roomLock[roomnumber]);	
		roomCondition[roomnumber]->Wait(roomLock[roomnumber]);
		wallpoketLock.Release();
		roomLock[roomnumber]->Release();
		if(testcontrol==6||testcontrol==7)
			printf("Doctor [%d] has left Examination Room [%d].\n", which, roomnumber);		
		if(testcontrol==6||testcontrol==7)
			printf("Doctor [%d] is going to their office.\n", which);
		
		currentThread->Yield(); //release the CPU
	}
}

